package com.et114.core.utility;

/*
 * Copyright (c) 2005 Allways, LLC. All rights reserved.
 * �������� 2005-11-22
 * ���ù����๹����
 */

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * 封装属性Properties类操作 @ <a href="PropertiesUtil.java.html"><b><i>View Source</i></b></a>
 * 
 * @作者 daomingw
 * @版本 1.0
 */
public class PropertiesUtil {
	ClassLoader	classLoader	= getClass ( ).getClassLoader ( );
	
	/**
	 * 从源属性类 复制至目标属性类
	 * 
	 * @param from
	 *            源Properties
	 * @param to
	 *            目标Properties
	 */
	public static void copyProperties ( Properties from , Properties to ) {
		Iterator itr = from.entrySet ( ).iterator ( );
		
		while (itr.hasNext ( )) {
			Map.Entry entry = ( Map.Entry ) itr.next ( );
			
			to.setProperty ( ( String ) entry.getKey ( ) , ( String ) entry
					.getValue ( ) );
		}
	}
	
	/**
	 * 把Map 转换成Properties
	 * 
	 * @param map
	 * @return
	 */
	public static Properties fromMap ( Map map ) {
		Properties p = new Properties ( );
		
		Iterator itr = map.entrySet ( ).iterator ( );
		
		while (itr.hasNext ( )) {
			Map.Entry entry = ( Map.Entry ) itr.next ( );
			
			p.setProperty ( ( String ) entry.getKey ( ) , ( String ) entry
					.getValue ( ) );
		}
		
		return p;
	}
	
	/**
	 * 把Properties转换成Map
	 * 
	 * @param p
	 * @param map
	 */
	public static void fromProperties ( Properties p , Map map ) {
		map.clear ( );
		
		Iterator itr = p.entrySet ( ).iterator ( );
		
		while (itr.hasNext ( )) {
			Map.Entry entry = ( Map.Entry ) itr.next ( );
			
			map.put ( entry.getKey ( ) , entry.getValue ( ) );
		}
	}
	
	/**
	 * 装载属性文件内容 成Properties
	 * 
	 * @param s
	 * @return
	 * @throws IOException
	 */
	public static Properties load ( String s ) throws IOException {
		Properties p = new Properties ( );
		
		load ( p , s );
		
		return p;
	}
	
	public static void load ( Properties p , String s ) throws IOException {
		if ( Validator.isNotNull ( s ) ) {
			/*
			 * s = UnicodeFormatter.toString(s); s = StringUtil.replace(s,
			 * "\\u003d", "="); s = StringUtil.replace(s, "\ ", "\n");
			 */
			s = Native2Ascii.encode ( s );
			p.load ( new ByteArrayInputStream ( s.getBytes ( ) ) );
		}
	}
	
	/**
	 * 合并2个Properties p1, Properties p2
	 * 
	 * @param p1
	 * @param p2
	 */
	public static void merge ( Properties p1 , Properties p2 ) {
		Enumeration enu = p2.propertyNames ( );
		
		while (enu.hasMoreElements ( )) {
			String key = ( String ) enu.nextElement ( );
			String value = p2.getProperty ( key );
			
			p1.setProperty ( key , value );
		}
	}
	
	public static String toString ( Properties p ) {
		StringBuffer sb = new StringBuffer ( );
		Enumeration enu = p.propertyNames ( );
		while (enu.hasMoreElements ( )) {
			String key = ( String ) enu.nextElement ( );
			
			sb.append ( key );
			sb.append ( StringPool.EQUAL );
			sb.append ( p.getProperty ( key ) );
			sb.append ( "\n" );
		}
		
		return sb.toString ( );
	}
	
	public static void trimKeys ( Properties p ) {
		Enumeration enu = p.propertyNames ( );
		
		while (enu.hasMoreElements ( )) {
			String key = ( String ) enu.nextElement ( );
			String value = p.getProperty ( key );
			
			String trimmedKey = key.trim ( );
			
			if ( ! key.equals ( trimmedKey ) ) {
				p.remove ( key );
				p.setProperty ( trimmedKey , value );
			}
		}
	}
	
}